
package com.bdtop.dbl.ui.activities.acccount;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.R;
import com.bdtop.dbl.model.Log;
import com.bdtop.dbl.server.ServerApiFactory;
import com.bdtop.dbl.server.response.LogListResponse;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.ui.adapters.LogListAdapter;
import com.bdtop.dbl.ui.view.TitleBar;
import com.bdtop.dbl.ui.view.XListView;
import com.bdtop.dbl.ui.view.XListView.IXListViewListener;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.NetworkUtils;
import com.bdtop.dbl.utils.Preferences;

import java.util.ArrayList;

public class LogActivity extends BaseActivity implements IXListViewListener {

    private TitleBar mTitleBar;
    private XListView listView;
    private LogListAdapter adapter;
    private long logCycle = 1 * 24 * 60 * 60;
    private int operType = 0;
    private long startTime, endTime;
    private ArrayList<Log> list;
    private int size = 1;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_log);

        initViews();
        queryData(size);
    }

    public static final void launch(Context context) {
        Intent intent = new Intent(context, LogActivity.class);
        ((Activity) context).startActivityForResult(intent, 0);
    }

    private void initViews() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle(R.string.oper_log);
        mTitleBar.setBelongActivity(this);

        listView = (XListView) findViewById(R.id.list_view);
        list = new ArrayList<Log>();
        adapter = new LogListAdapter(this);
        adapter.setList(list);
        listView.setAdapter(adapter);
        listView.setXListViewListener(this);
        listView.setPullLoadEnable(false);
    }

    private void queryData(int size) {
        startTime = System.currentTimeMillis() / 1000 - size * logCycle;
        endTime = System.currentTimeMillis() / 1000 - (size - 1) * logCycle;

        String sign = Preferences.getLoginSign(this);
       // showWaitingDialog(R.string.loading, true, false);
        ServerApiFactory.getApiImpl().getLogList(sign, String.valueOf(startTime),
                String.valueOf(endTime), new RequestCallback() {

                    @Override
                    public void onSuccess(String rawData, Object response) {
                        LogListResponse listResponse = (LogListResponse) response;
                        if (listResponse.getStatus().equals(Constants.REQUEST_SUCCESS)) {
                            loadData(listResponse);
                         }else{
                             showToast(R.string.no_more_log_data);
                            }
                        setPageSize();
                        onLoad();
                    }

                    @Override
                    public void onError(int statusCode, Throwable e, String rawData) {
                        if(NetworkUtils.isNetConnect(getApplicationContext())){
                            showToast(R.string.oper_error);
                        }else{
                            showToast(R.string.net_error);
                             }
                    }
                });
    }

    private void setPageSize(){
        size += 1;
    }
    
    private void loadData(LogListResponse listResponse) {

        if(operType==1){
            list.clear();
        }
        
        if (null != listResponse.getLogdata()) {
            list.addAll(listResponse.getLogdata());
            adapter.notifyDataSetChanged();
            listView.setPullLoadEnable(true);
        } else {
            if (operType == 0) {
                // no data
            } else {
                showToast(R.string.no_more_log_data);
            }
        }
        adapter.setList(list);
        adapter.notifyDataSetChanged();
    }

    private void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
        listView.setRefreshTime(getString(R.string.just_now));
    }

    @Override
    public void onRefresh() {
        size=1;
        operType = 1;
        queryData(size);
    }

    @Override
    public void onLoadMore() {
        operType = 2;
        queryData(size);
    }

}
